﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PH.Model.Schools;
namespace PH.Data.Schools
{
    public class GradeCommand
    {
        public static GradeCollection GetGrades() 
        {
            var result = new GradeCollection();
            using (DBCentre db = new DBCentre())
            {
                IQueryable<Grade> q = db.DataContext.Grades;
                q = q.OrderBy(p => p.Code);
                var n = q.Count();
                if (n > 0)
                {
                    var lst = q.ToList();
                    result = Convertor.ConvertCollection<GradeModel, Grade, GradeCollection>(lst);
                    result.TotalRecordsCount = n;
                }
            }
            return result;
        }

        public static GradeCollection GetGrades(int schType)
        {
            var result = new GradeCollection();
            using (DBCentre db = new DBCentre())
            {
                var st = db.DataContext.SchoolTypes.FirstOrDefault(p => p.ID == schType);
                if (st != null)
                {
                    IQueryable<Grade> q = db.DataContext.Grades.Where(p => p.Code >= st.MinGrade && p.Code <= st.MaxGrade);
                    q = q.OrderBy(p => p.Code);
                    var n = q.Count();
                    if (n > 0)
                    {
                        var lst = q.ToList();
                        result = Convertor.ConvertCollection<GradeModel, Grade, GradeCollection>(lst);
                        result.TotalRecordsCount = n;
                    }
                }
            }
            return result;
        }
    }
}
